<?php

namespace app\product\logic;

use app\common\library\Excel;
use app\common\service\UploadService;
use app\data\util\MqQueueName;
use app\systems\logic\MqTaskPushLogic;
use app\product\logic\InventoryLogic;
use think\facade\Db;

class ProductNewLogic extends BasicLogic
{


    /**
     * @desc   获取产品库存列表
     * @author luckyXiong
     * @date   2024-07-24
     */
    public function getList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $spu             = getIsset($params,'spu');
        $is_new          = getIsset($params,'is_new');
        $is_current      = getIsset($params,'is_current');
        $create_time     = getIsset($params,'create_time');
        $check_status    = getIsset($params,'check_status');
        $where = [];

        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }

        if($is_current){
            $where[] = ['is_current', '=', $is_current];
        }

        if($is_new){
            $where[] = ['is_new', '=', $is_new];
        }

        if($check_status){
            $where[] = ['check_status', '=', $check_status];
        }

        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }


        $order = ['update_time DESC, create_time DESC'];
        $res   = Db::name('product_check')
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];

        $data = [
            'list'                    => $list,
            'total'                   => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);

    }


    /**
     * @desc   获取产品库存导入
     * @author luckyXiong
     * @date   2024-07-24
     */
    public function import_info($url){
        $data             = Excel::getExcelData('.' . '/' . $url);
        $excelData        = isset($data[0]) ? $data[0] : $data;
        $excelData        = array_values($excelData);
        if(!$excelData)    return json(DefaultResponse(201, false, '暂无数据!', null));
        foreach($excelData as $v){
            MqTaskPushLogic::mq_task_push(
                '',
                MqQueueName::QUEUE_ORDER_NEW_PRODUCT_UPLOAD_QUEUE,
                $v,
                false
            );
        }
    }


    /**
     * @desc   获取产品新品消费方法
     * @author luckyXiong
     * @date   2024-07-24
     */
    public function uploadProductConsumer($params){
                 $product_images            = $params['H']??'';
                 if($product_images){
                     $product_images = (new InventoryLogic())->uploadImage(['url'=>$product_images]);
                 }

                 $product_size_images      = $params['R']??'';

                 if($product_size_images){
                     $product_size_images = (new InventoryLogic())->uploadImage(['url'=>$product_size_images]);
                 }

                 $update_data = [
                    'create_time'           =>date("Y-m-d H:i:s", time()),
                    'is_new'                =>$params['B']??'',
                    'develper'              =>$params['C']??'',
                    'product_name'          =>$params['D']??'',
                    'product_attr_type'     =>$params['E']??'',
                    'color_variant'         =>$params['F']??'',
                    'size_variant'          =>$params['G']??'',
                    'product_images'        =>$product_images,
                    'purchase_cost'         =>$params['I']??'',
                    'sale_cost'             =>$params['J']??'',
                    'gross_profit'          =>$params['M']??'',
                    'gross_profit_rate'     =>$params['N']??'',
                    'is_current'            =>$params['Q']??'',
                    'base_weight'           =>$params['K']??'',
                    'shipping_fee'          =>$params['L']??'',
                    'supply_name'           =>$params['O']??'',
                    'supply_link'           =>$params['P']??'',
                    'product_size_images'   =>$product_size_images,
                ];

                $shipping_fee = $update_data['shipping_fee'];

                if(!$shipping_fee){
                    $shipping_fee = 0;
                }

                $base_weight      = $update_data['base_weight'];
                if($base_weight){
                    $shipping_fee = ($base_weight*78)+18;
                    $shipping_fee = getRes($shipping_fee);
                }

                if($shipping_fee){
                    $update_data['shipping_fee'] = $shipping_fee;
                }


                $purchase_cost = $update_data['purchase_cost'];


                if(!$purchase_cost){
                    $purchase_cost = 0;
                }

                $sale_cost = $update_data['sale_cost'];
                if(!$sale_cost){
                    $sale_cost = 0;
                }


                if($sale_cost){
                    $gross_profit = ($sale_cost*6.8)-$purchase_cost-$shipping_fee;
                    $gross_profit = getRes($gross_profit);
                }

                $gross_profit_rate = $update_data['gross_profit_rate'];

                if(!$gross_profit_rate){
                    $gross_profit_rate = 0;
                }

                if($sale_cost && $purchase_cost){
                    $real_sale_cost    = $sale_cost*6.8;
                    $gross_profit_rate =(($real_sale_cost-$purchase_cost)/$real_sale_cost)*100;
                    $gross_profit_rate = getRes($gross_profit_rate).'%';
                }

                if($gross_profit){
                    $update_data['gross_profit'] = $gross_profit;
                }

                if($gross_profit_rate){
                    $update_data['gross_profit_rate'] = $gross_profit_rate;
                }
               Db::name('product_check')->insert($update_data);
               return  true;
    }


    /**
     * @desc   产品上新新增接口
     * @author luckyXiong
     * @date   2024-07-25
     */
    public function add_info($params=[]){
           $product_attr_type   = $params['product_attr_type']??'';
           $product_name        = $params['product_name']??'';
           $size_variant        = $params['size_variant']??'';
           $color_variant       = $params['color_variant']??'';
           $product_size_images = $params['product_size_images']??'';
           $product_images      = $params['product_images']??'';
           $purchase_cost       = $params['purchase_cost']??'';
           $sale_cost           = $params['sale_cost']??'';
           $gross_profit        = $params['gross_profit']??'';
           $gross_profit_rate   = $params['gross_profit_rate']??'';
           $is_current          = $params['is_current']??'';
           $is_new              = $params['is_new']??'';
           $base_weight         = $params['base_weight']??'';
           $supply_name         = $params['supply_name']??'';
           $supply_link         = $params['supply_link']??'';
           $develper            = $params['develper']??'';


       $update_data = [
        'product_attr_type'   => $product_attr_type,
        'product_name'        => $product_name,
        'size_variant'        => $size_variant,
        'color_variant'       => $color_variant,
        'product_size_images' => $product_size_images,
        'product_images'      => $product_images,
        'purchase_cost'       => $purchase_cost,
        'sale_cost'           => $sale_cost,
        'gross_profit'        => $gross_profit,
         'gross_profit_rate'  => $gross_profit_rate,
          'is_current'        => $is_current,
          'is_new'            => $is_new,
          'base_weight'       => $base_weight,
          'supply_name'       => $supply_name,
          'supply_link'       => $supply_link,
          'develper'          => $develper,
          'create_time'       => date("Y-m-d H:i:s", time()),
         ];

          $shipping_fee = 0;

          if($base_weight){
              $shipping_fee = ($base_weight*78)+18;
              $shipping_fee = getRes($shipping_fee);
          }

          if($shipping_fee){
              $update_data['shipping_fee'] = $shipping_fee;
          }


          if(!$purchase_cost){
              $purchase_cost = 0;
          }


          if(!$sale_cost){
              $sale_cost = 0;
          }


          if($sale_cost){
              $gross_profit = ($sale_cost*6.8)-$purchase_cost-$shipping_fee;
              $gross_profit = getRes($gross_profit);
          }

            $gross_profit_rate = 0;


            if($sale_cost && $purchase_cost){
              $real_sale_cost    = $sale_cost*6.8;
              $gross_profit_rate =(($real_sale_cost-$purchase_cost)/$real_sale_cost)*100;
              $gross_profit_rate = getRes($gross_profit_rate).'%';
            }

            if($gross_profit){
              $update_data['gross_profit'] = $gross_profit;
            }

           if($gross_profit_rate){
              $update_data['gross_profit_rate'] = $gross_profit_rate;
           }

           //数据入库;
           Db::name('product_check')->insert(
               $update_data
           );
          return  $this->jsonMessage(200,true,'操作成功',null);
    }


    /**
     * @desc    产品上新详情接口
     * @author  luckyXiong
     * @date    2024-07-25
     */
    public function get_info($params=[]){
           $id   = $params['id']??'';
           $info = Db::name('product_check')->where('id',$id)->find();
           return  $this->jsonMessage(200,true,'获取成功',$info);
    }


    /**
     * @desc    上新编辑接口
     * @author  luckyXiong
     * @date    2024-07-25
     */
    public function edit_info($params=[]){
        $id                  = $params['id']??'';
        $product_attr_type   = $params['product_attr_type']??'';
        $product_name        = $params['product_name']??'';
        $size_variant        = $params['size_variant']??'';
        $color_variant       = $params['color_variant']??'';
        $product_size_images = $params['product_size_images']??'';
        $product_images      = $params['product_images']??'';
        $purchase_cost       = $params['purchase_cost']??'';
        $sale_cost           = $params['sale_cost']??'';
        $gross_profit        = $params['gross_profit']??'';
        $gross_profit_rate   = $params['gross_profit_rate']??'';
        $is_current          = $params['is_current']??'';
        $is_new              = $params['is_new']??'';
        $base_weight         = $params['base_weight']??'';
        $supply_name         = $params['supply_name']??'';
        $supply_link         = $params['supply_link']??'';
        $develper            = $params['develper']??'';



        $shipping_fee = 0;

        if($base_weight){
            $shipping_fee = ($base_weight*78)+18;
            $shipping_fee = getRes($shipping_fee);
        }

        if($shipping_fee){
            $update_data['shipping_fee'] = $shipping_fee;
        }


        if(!$purchase_cost){
            $purchase_cost = 0;
        }



        if(!$sale_cost){
            $sale_cost = 0;
        }


        if($sale_cost){
            $gross_profit = ($sale_cost*6.8)-$purchase_cost-$shipping_fee;
            $gross_profit = getRes($gross_profit);
        }

        $gross_profit_rate = 0;


        if($sale_cost && $purchase_cost){
            $real_sale_cost    = $sale_cost*6.8;
            $gross_profit_rate =(($real_sale_cost-$purchase_cost)/$real_sale_cost)*100;
            $gross_profit_rate = getRes($gross_profit_rate).'%';
        }

        if($gross_profit){
            $update_data['gross_profit'] = $gross_profit;
        }

        if($gross_profit_rate){
            $update_data['gross_profit_rate'] = $gross_profit_rate;
        }





        $update_data = [
            'product_attr_type'   => $product_attr_type,
            'product_name'        => $product_name,
            'size_variant'        => $size_variant,
            'color_variant'       => $color_variant,
            'product_size_images' => $product_size_images,
            'product_images'      => $product_images,
            'purchase_cost'       => $purchase_cost,
            'sale_cost'           => $sale_cost,
            'gross_profit'        => $gross_profit,
            'gross_profit_rate'   => $gross_profit_rate,
            'is_current'          => $is_current,
            'is_new'              => $is_new,
            'base_weight'         => $base_weight,
            'supply_name'         => $supply_name,
            'supply_link'         => $supply_link,
            'develper'            => $develper,
            'update_time'         => date("Y-m-d H:i:s", time()),
        ];
        //数据更新入库;
        Db::name('product_check')->where(['id'=>$id])->update($update_data);
        return  $this->jsonMessage(200,true,'编辑成功',null);
    }


    /**
     * @desc    产品审核接口
     * @author  luckyXiong
     * @date    2024-07-25
     */
    public function check_info($params=[]){
           $id     = $params['id']??'';
           if(!$id){
               return  $this->jsonMessage(201,false,'ID不能为空',null);
           }

           $check_status = $params['check_status']??'';

           if($check_status==2){
               $spu     =  $params['spu']??'';
               Db::name('product_check')->where(['id'=>$id])->update([
                   'spu'           =>$spu,
                   'check_status'  =>$check_status,
                   'update_time'   =>date("Y-m-d H:i:s", time()),
               ]);

               //审核通过填写供应商基础信息;
               $info = Db::name('product_check')->where(['id'=>$id])->find();
               if($spu){
                  $exist_info =  Db::name('supply_info')->where(['spu'=>$spu,'supply_link'=>$info['supply_link']])->find();
                  if(!$exist_info){
                      $res = Db::name('supply_info')->insertGetId([
                          'spu'        =>$spu,
                          'supply_link'=>$info['supply_link'],
                          'supply_name'=>$info['supply_name'],
                      ]);
                      Db::name('product_check')->where(['id'=>$id])->update(['source_link'=>$res]);
                  }
               }
           }else{
                $remarks =  $params['remarks']??'';
                Db::name('product_check')->where(['id'=>$id])->update([
                   'remarks'      =>$remarks,
                   'check_status' =>$check_status,
                   'update_time'  =>date("Y-m-d H:i:s", time()),
               ]);
           }
          return  $this->jsonMessage(200,true,'审核成功',null);
    }










}